package com.ruoyi.program.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.program.domain.WxChapter;
import com.ruoyi.program.service.IWxChapterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 章节管理Controller
 *
 * @author ruoyi
 * @date 2025-09-18
 */
@RestController
@RequestMapping("/program/chapter")
public class WxChapterController extends BaseController
{
    @Autowired
    private IWxChapterService wxChapterService;

    /**
     * 查询章节管理列表
     */
    @PreAuthorize("@ss.hasPermi('program:chapter:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxChapter wxChapter)
    {
        startPage();
        List<WxChapter> list = wxChapterService.selectWxChapterList(wxChapter);
        return getDataTable(list);
    }

    /**
     * 导出章节管理列表
     */
    @PreAuthorize("@ss.hasPermi('program:chapter:export')")
    @Log(title = "章节管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxChapter wxChapter)
    {
        List<WxChapter> list = wxChapterService.selectWxChapterList(wxChapter);
        ExcelUtil<WxChapter> util = new ExcelUtil<WxChapter>(WxChapter.class);
        util.exportExcel(response, list, "章节管理数据");
    }

    /**
     * 获取章节管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('program:chapter:query')")
    @GetMapping(value = "/{chapterId}")
    public AjaxResult getInfo(@PathVariable("chapterId") Long chapterId)
    {
        return success(wxChapterService.selectWxChapterByChapterId(chapterId));
    }

    /**
     * 新增章节管理
     */
    @PreAuthorize("@ss.hasPermi('program:chapter:add')")
    @Log(title = "章节管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxChapter wxChapter)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();

        // 为公共字段赋值
        wxChapter.setCreateBy(username);  // 创建人
        wxChapter.setUpdateBy(username);  // 更新人（新增时与创建人一致）
        wxChapter.setCreateTime(new Date());  // 创建时间（可选，BaseEntity可能默认自动填充）
        wxChapter.setUpdateTime(new Date());  // 更新时间
        return toAjax(wxChapterService.insertWxChapter(wxChapter));
    }

    /**
     * 修改章节管理
     */
    @PreAuthorize("@ss.hasPermi('program:chapter:edit')")
    @Log(title = "章节管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxChapter wxChapter)
    {
        return toAjax(wxChapterService.updateWxChapter(wxChapter));
    }

    /**
     * 删除章节管理
     */
    @PreAuthorize("@ss.hasPermi('program:chapter:remove')")
    @Log(title = "章节管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{chapterIds}")
    public AjaxResult remove(@PathVariable Long chapterIds)
    {
        return toAjax(wxChapterService.deleteWxChapterByChapterId(chapterIds));
    }
}
